﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Data.OleDb;


namespace App1
{
    public partial class frmOpen : Form
    {
        public frmOpen()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text.ToString() + ".txt";
            try
            {
                GameChess.OpenGame();
                // GameChess.NewGame();
                StreamReader Re = File.OpenText(textBox1.Text);
                for (int j = 0; j < 2; j++)
                {
                    string s;

                    // player 1
                    s = Re.ReadLine().ToString();
                    int i = int.Parse(s[0].ToString());

                    string state="";
                    int d = 0, c = 0;
                    bool t;

                  

                    // pawns /2-3-4-5-6
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Pawns[0].OpenRow(d);
                    GameChess.Player[i].Pawns[0].OpenCol(c);
                    GameChess.Player[i].Pawns[0].State = t;
                    GameChess.Player[i].Pawns[0].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Pawns[1].OpenRow(d);
                    GameChess.Player[i].Pawns[1].OpenCol(c);
                    GameChess.Player[i].Pawns[1].State = t;
                    GameChess.Player[i].Pawns[1].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Pawns[2].OpenRow(d);
                    GameChess.Player[i].Pawns[2].OpenCol(c);
                    GameChess.Player[i].Pawns[2].State = t;
                    GameChess.Player[i].Pawns[2].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Pawns[3].OpenRow(d);
                    GameChess.Player[i].Pawns[3].OpenCol(c);
                    GameChess.Player[i].Pawns[3].State = t;
                    GameChess.Player[i].Pawns[3].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Pawns[4].OpenRow(d);
                    GameChess.Player[i].Pawns[4].OpenCol(c);
                    GameChess.Player[i].Pawns[4].State = t;
                    GameChess.Player[i].Pawns[4].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    // Guards 7-8
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Guards[0].OpenRow(d);
                    GameChess.Player[i].Guards[0].OpenCol(c);
                    GameChess.Player[i].Guards[0].State = t;
                    GameChess.Player[i].Guards[0].pic.Visible = t;
                    Board.Square[d, c].State = t;


                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Guards[1].OpenRow(d);
                    GameChess.Player[i].Guards[1].OpenCol(c);
                    GameChess.Player[i].Guards[1].State = t;
                    GameChess.Player[i].Guards[1].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    // Elephants 9-10
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Elephants[0].OpenRow(d);
                    GameChess.Player[i].Elephants[0].OpenCol(c);
                    GameChess.Player[i].Elephants[0].State = t;
                    GameChess.Player[i].Elephants[0].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Elephants[1].OpenRow(d);
                    GameChess.Player[i].Elephants[1].OpenCol(c);
                    GameChess.Player[i].Elephants[1].State = t;
                    GameChess.Player[i].Elephants[1].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    // Horses 11-12
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Horses[0].OpenRow(d);
                    GameChess.Player[i].Horses[0].OpenCol(c);
                    GameChess.Player[i].Horses[0].State = t;
                    Board.Square[d,c].State = t;
                    GameChess.Player[i].Horses[0].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Horses[1].OpenRow(d);
                    GameChess.Player[i].Horses[1].OpenCol(c);
                    GameChess.Player[i].Horses[1].State = t;
                    GameChess.Player[i].Horses[1].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    //Cannons 12-14
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Cannons[0].OpenRow(d);
                    GameChess.Player[i].Cannons[0].OpenCol(c);
                    GameChess.Player[i].Cannons[0].State = t;
                    GameChess.Player[i].Cannons[0].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Cannons[1].OpenRow(d);
                    GameChess.Player[i].Cannons[1].OpenCol(c);
                    GameChess.Player[i].Cannons[1].State = t;
                    GameChess.Player[i].Cannons[1].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    // Rooks 15-16
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Rooks[0].OpenRow(d);
                    GameChess.Player[i].Rooks[0].OpenCol(c);
                    GameChess.Player[i].Rooks[0].State = t;
                    GameChess.Player[i].Rooks[0].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].Rooks[1].OpenRow(d);
                    GameChess.Player[i].Rooks[1].OpenCol(c);
                    GameChess.Player[i].Rooks[1].State = t;
                    GameChess.Player[i].Rooks[1].pic.Visible = t;
                    Board.Square[d, c].State = t;

                    // King 17
                    s = Re.ReadLine().ToString();
                    d = int.Parse(s[0].ToString());
                    c = int.Parse(s[2].ToString());
                    for (int x = 4; x < s.Length; x++)
                    {
                        state = state + s[x];
                    }
                    t = bool.Parse(state.ToString());
                    state = "";
                    GameChess.Player[i].King.OpenRow(d);
                    GameChess.Player[i].King.OpenCol(c);
                    GameChess.Player[i].King.State = t;
                    GameChess.Player[i].King.pic.Visible = t;
                    Board.Square[d, c].State = t;
                }
                GameChess.UdateOpen();
                GameChess.Set_Playing(1);
                GameChess.ChangeTurn();
                Re.Close();
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("Tên ván đấu bạn mở không tồn tại");
            }
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
